package cn.qh.com.exception;


/**
 * GlobalExceptionAdvice 异常全局处理类
 *
 * @author 孟佳林
 * @since 2021-11-26 09:46:13
 */

import cn.qh.com.vo.CommonResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.util.List;

@ControllerAdvice
public class GlobalExceptionAdvice {
    @Resource
    private ExceptionCodeConfiguration exceptionCodeConfiguration;

//    //处理的系统未知的异常
//    @ExceptionHandler({Exception.class})
//    public ResponseEntity<CommonResponse> handleException(HttpServletRequest
//                                                                  request,
//                                                          Exception e) {
//        String url = request.getRequestURL().toString();
//        String method = request.getMethod();
//        String requestUrl = method + "-" + url;
//        //封装的错误信息
//        UnifyResponse unifyResponse =
//                UnifyResponse.builder().code(9999)
//                        .message("服务器异常").request(requestUrl).build();
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.setContentType(MediaType.APPLICATION_JSON); //数据格式
//        HttpStatus httpStatus = HttpStatus.resolve(500); //编码
//        ResponseEntity responseEntity =
//                new ResponseEntity(unifyResponse, httpHeaders, httpStatus);
//        return responseEntity; //返回的响应信息:封装好的错误信息
//    }
//
//    //处理的系统已知的异常(继承HttpException的异常)
//    @ExceptionHandler({HttpException.class})
//    public ResponseEntity<UnifyResponse>
//    handleHttpException(HttpServletRequest request, HttpException e) {
//        String url = request.getRequestURL().toString(); //拼接请求的url
//        String method = request.getMethod();
//        String requestUrl = method + "-" + url;
//        int codeValue = e.getCode(); //根据错误编号获得异常的错误信息
//        String msgValue = exceptionCodeConfiguration.getMessage(codeValue);
////封装的错误信息
//        UnifyResponse unifyResponse =
//                UnifyResponse.builder().code(codeValue)
//                        .message(msgValue).request(requestUrl).build();
//        HttpHeaders httpHeaders = new HttpHeaders();
//
//        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
//        HttpStatus httpStatus = HttpStatus.resolve(e.getHttpStatusCode());
//        ResponseEntity responseEntity =
//                new ResponseEntity(unifyResponse, httpHeaders, httpStatus);
//        return responseEntity; //返回的响应信息:封装好的错误信息
//    }
//
//    //处理系统参数验证异常
//    @ExceptionHandler(MethodArgumentNotValidException.class)
//    @ResponseBody
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public UnifyResponse
//    handleMethodArgumentNotValidException(HttpServletRequest
//                                                  request,
//                                          MethodArgumentNotValidException e) {
//        List<FieldError> objectErrorList = e.getBindingResult().getFieldErrors();//获得所有的错误信息
//        String message = ""; //遍历错误信息,对错误信息进行拼接
//        for (ObjectError objectError : objectErrorList) {
//            message += objectError.getDefaultMessage() + ";";
//        }
//        //获得报错的请求的地址
//        String requestURL = request.getMethod() + "-" + request.getRequestURL();
//        //封装unifyResponse对象
//        UnifyResponse unifyResponse = UnifyResponse.builder().code(10000)
//                .message(message).request(requestURL).build();
//        return unifyResponse; //返回信息
//    }
//
//    //处理系统参数验证异常(Rest风格传递参数)
//    @ExceptionHandler(value = ConstraintViolationException.class)
//    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
//    @ResponseBody
//    public UnifyResponse handlerConstraintViolationException(HttpServletRequest
//                                                                     request,
//                                                             ConstraintViolationException e) {
//        //获得报错的请求的地址
//        String requestURL = request.getMethod() + "-" + request.getRequestURL();
//        //错误信息
//        String message = e.getMessage();
//        //封装返回信息
//        UnifyResponse unifyResponse = UnifyResponse.builder()
//                .code(10000).request(requestURL).message(message).build();
//        return unifyResponse;
//    }
}

